package com.sp.system.user.customer.controller;

import com.sp.system.user.core.annotation.NotAuthentication;
import com.sp.system.user.core.entity.User;
import com.sp.system.user.core.utils.ContentResult;
import com.sp.system.user.core.utils.DateUtil;
import com.sp.system.user.core.utils.security.JwtTokenUtil;
import com.sp.system.user.customer.service.IUserService;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * @Describle 用户信息类
 * @author chenganhai
 * @date 2019年9月6日
 */
@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    IUserService userService;

    /**
     * 根据登录名 获取登录信息
     * @param loginName
     * @return
     */
    @NotAuthentication
    @RequestMapping("/getUserByLoginName")
    public Object getUserByLoginName(String loginName) {
        User user = userService.getUserByLoginName(loginName);
        return ContentResult.successResult(user,"登录用户查询结果");
    }


    @NotAuthentication
    @RequestMapping("/registerUser")
    public Object registerUser(@RequestBody User user){
        user.setCreateDate(DateUtil.getNowDate());
        boolean saveFlag = userService.save(user);
        if(saveFlag){
            return ContentResult.successResult(user,"");
        }
        return ContentResult.errorResult(user,"注册信息失败");
    }

    @RequestMapping("/getUserInfo")
    public Object getUserInfo(HttpServletRequest request,User user){
        String loginName = null;
        if(user == null || user.getLoginName() == null || "".equals(user.getLoginName()) ){
            String accessToken = request.getHeader(JwtTokenUtil.tokenHeader);
            if (null!=accessToken && accessToken.startsWith(JwtTokenUtil.tokenPrefix)) {
                Claims claims = JwtTokenUtil.getClaimsFromToken(accessToken);
                loginName = claims.getSubject();
            }
        }
        user = userService.getUserByLoginName(loginName);

        if(user != null){
            return ContentResult.successResult(user,"");
        }
        return ContentResult.errorResult(user,"获取用户信息失败");
    }

    @RequestMapping("/logout")
    public Object logout(HttpServletRequest request,User user){

        if(user == null || user.getLoginName() == null || "".equals(user.getLoginName()) ){
            String accessToken = request.getHeader(JwtTokenUtil.tokenHeader);
            if (null!=accessToken && accessToken.startsWith(JwtTokenUtil.tokenPrefix)) {
                Claims claims = JwtTokenUtil.getClaimsFromToken(accessToken);
//                loginName = claims.getSubject();
            }
        }

        if(user != null){
            return ContentResult.successResult(user,"注销登录成功");
        }
        return ContentResult.errorResult(user,"注销登录信息失败");
    }

}
